import java.io.File;
import java.io.FileInputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import request.ServerRequestKind;
import socket.SocketOutput;

public class Updater extends TimerTask {

	private String resultDir;
	private SocketOutput out;
	private Timer timer = new Timer();

	public Updater(String resultDir, SocketOutput socketOutput) {
		this.resultDir = resultDir;
		this.out = socketOutput;
		timer.schedule(this, 3000, 3000);
	}

	@Override
	public void run() {
		File path = new File(resultDir);
		StringBuilder error = new StringBuilder();

		try {
			for (File file : path.listFiles()) {
				if (file.getAbsolutePath().endsWith(".xml")) {
					Node node = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file).getFirstChild();
					if (node.getNodeName().equalsIgnoreCase("testsuite") && (Integer.parseInt(((Element) node).getAttribute("errors")) > 0)) {
						out.write(ServerRequestKind.TEST_ERROR.ordinal(), new FileInputStream(file));
					}
				}

			}
		} catch (Exception e) {
		}
	}

}
